package com.cn.jinhao.hotline.provider.huawu.common;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;

public class CtiCommon {
    private static int fixCode = 0;
    public static JSONObject invokeAjaxCall(String url, String jsonParams)
    {
        try{
            HttpResponse response = HttpRequest.post(url).body(jsonParams).execute();
            int respCode = response.getStatus();
            if(respCode == HttpStatus.HTTP_OK)
            {
                String result = response.body();
                JSONObject obj = new JSONObject(result);
                return obj;
            }
        }
        catch (Exception e)
        {
            e.getStackTrace();
        }
        return null;
    }

    public static String generateCode() throws Exception {
        fixCode++;
        if (fixCode < 10) {
            return "99900" + fixCode;
        }else if (fixCode < 100) {
            return "9990" + fixCode;
        }else if (fixCode < 1000){
            return "999" + fixCode;
        }else {
            throw new Exception("自动产生CODE已经超出最大范围，请置空初始数，调用CtiCommon.initFixCode()方法.");
        }
    }

    public static void initFixCode() {
        fixCode = 0;
    }

}
